package com.kgcx.common.utils.date;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author panjin.
 * @date 2017/12/18.
 */
@Slf4j
public class DataValidator {

    /**
     * 利用正则表达式检查是否完整匹配
     *
     * @param text
     * @param reg
     * @return
     */
    public static final boolean isMatch(String text, String reg) {
        if (StringUtils.isNotEmpty(text) && StringUtils.isNotBlank(reg)) {
            Pattern p = Pattern.compile(reg);
            Matcher m = p.matcher(text);
            return m.matches();
        }
        return false;
    }

    /**
     * 判断字串是否符合yyyy-MM-dd HH:mm:ss格式
     *
     * @param aDateStr
     * @return
     */
    public static final boolean isLongDateStr(String aDateStr) {
        try {
            DateUtils.parseLongDateString(aDateStr);
        } catch (ParseException e) {
            log.warn("isLongDateStr failed", e);
            return false;
        }
        return true;
    }

    /**
     * 判断字串是否符合yyyy-MM-dd格式
     *
     * @param aDateStr
     * @return
     */
    public static final boolean isShortDateStr(String aDateStr) {
        try {
            DateUtils.parseShortDateString(aDateStr);
        } catch (ParseException e) {
            log.warn("isShortDateStr failed", e);
            return false;
        }
        return true;
    }

    /**
     * 判断字串是否符合yyyyMMdd格式
     *
     * @param aDateStr
     * @return
     */
    public static final boolean isMailDateDtPartStr(String aDateStr) {
        try {
            DateUtils.parseMailDateDtPartString(aDateStr);
        } catch (ParseException e) {
            log.warn("isMailDateDtPartStr failed", e);
            return false;
        }
        return true;
    }

    /**
     * 判断字串是否符合指定的日期格式
     *
     * @param aDateStr
     * @return
     */
    public static final boolean isDateStrMatched(String aDateStr, String formatter) {
        try {
            DateUtils.parser(aDateStr, formatter);
        } catch (ParseException e) {
            log.warn("isDateStrMatched failed", e);
            return false;
        }
        return true;
    }

    /**
     * 检查字串是否符合yyyy-MM-dd格式
     *
     * @param aDateStr
     */
    public static final void checkShortDateStr(String aDateStr) {
        Assert.isTrue(isShortDateStr(aDateStr), "The str-'" + aDateStr + "' must match 'yyyy-MM-dd' format.");
    }

    private DataValidator() {
    }
}
